package com.factory.code.lib;

import com.factory.code.service.model.CodeTableColumnModel;
import com.factory.code.service.model.CodeTableModel;
import com.factory.code.service.model.NameAndValueModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class CustomTemplateManager {
    private static final Logger logger = LoggerFactory.getLogger(CustomTemplateManager.class);

    private List<NameAndValueModel> method = null;

    public List<NameAndValueModel> getMethodList() {
        if (method == null) {
            method = new ArrayList<>();
            method.add(new NameAndValueModel("获取系统时间", "getCurrentTime"));
            method.add(new NameAndValueModel("根据文件目录获取包路径", "getPackageNameByFilePath"));
            method.add(new NameAndValueModel("根据数据库类型导入名路径", "getImportPackageByDbType"));
        }
        return method;
    }

    public String executeMethod(String method, String param, CodeTableModel tableModel) {
        String content = "";
        if (StringUtils.isEmpty(method)) {
            return content;
        }
        switch (method) {
            case "getCurrentTime":
                content = getCurrentTime(param, tableModel);
                break;
            case "getPackageNameByFilePath":
                content = getPackageNameByFilePath(param, tableModel);
                break;
            case "getImportPackageByDbType":
                content = getImportPackageByDbType(param, tableModel);
                break;
            default:
                logger.info("[自定义模板] 系统模板不存在    method={}", method);
        }
        return content;
    }

    /**
     * 根据文件目录获取包路径
     */
    public String getPackageNameByFilePath(String param, CodeTableModel tableModel) {
        return tableModel.getContext().getPackagePath();
    }

    /**
     * 根据数据库类型导入名路径
     */
    public String getImportPackageByDbType(String param, CodeTableModel tableModel) {
        StringBuffer sb = new StringBuffer();
        Boolean utilDate = false;
        Boolean mathBigDecimal = false;
        for (CodeTableColumnModel tableColumn : tableModel.getTableColumns()) {
            if ("Date".equals(tableColumn.getAttrType())) {
                if (!utilDate) {
                    sb.append("import java.util.Date;\n");
                    utilDate = true;
                }
            } else if ("BigDecimal".equals(tableColumn.getAttrType())) {
                if (!mathBigDecimal) {
                    mathBigDecimal = true;
                    sb.append("import java.math.BigDecimal;\n");
                }
            }
        }
        return sb.toString();
    }

    public String getCurrentTime(String param, CodeTableModel tableModel) {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }
}
